package com.oocl.euc.filter;

import com.oocl.csc.frm.sso.exceptions.SSOException;
import com.oocl.csc.frm.sso.filter.SSOBaseFilter;
import com.oocl.csc.frm.sso.util.SSOHelperUtils;
import com.oocl.euc.authentication.tool.WebHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SSOAuthFilter extends SSOBaseFilter {
    private static Logger logger= LoggerFactory.getLogger(SSOAuthFilter.class);
    private static final String CLASS_NAME = SSOAuthFilter.class.getSimpleName();
    private WebHelper webHelper;

    public SSOAuthFilter() {
    }

    public SSOAuthFilter(WebHelper webHelper) {
        this.webHelper = webHelper;
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        String METHOD_NAME = "doFilter(ServletRequest, ServletResponse, FilterChain)";
        HttpServletRequest httpRequest = (HttpServletRequest)request;
        HttpServletResponse httpResponse = (HttpServletResponse)response;

        if(webHelper.isRest(httpRequest)  || webHelper.isSkip(httpRequest)){
            chain.doFilter(request, response);
        } else {
            authenticationToken(request, response, chain, httpRequest, httpResponse);
        }
    }

    private void authenticationToken(ServletRequest request, ServletResponse response, FilterChain chain, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException {
        try {
            logger.info("Begin SSO authentication...");
            if (this.getSSOConfigComponent().isSsoEnabled()) {
                if (this.doIgnoreFilter(httpRequest, httpResponse)) {
                    chain.doFilter(request, response);
                    return;
                }

                this.doBeforeCheckTokenFromCookie(httpRequest, httpResponse);
                boolean isTokenValid = this.doCheckTokenFromCookie(httpRequest, httpResponse);
                this.doAfterCheckTokenFromCookie(httpRequest, httpResponse);
                if (!isTokenValid) {
                    this.doBeforeCheckOpenToken(httpRequest, httpResponse);
                    this.doCheckOpenToken(httpRequest, httpResponse);
                    this.doAfterCheckOpenToken(httpRequest, httpResponse);
                }
            }
        } catch (SSOException var8) {
            logger.error(CLASS_NAME + " SSO authentication failed...", var8);
            SSOHelperUtils.getLogger().logError(CLASS_NAME + "." + "doFilter(ServletRequest, ServletResponse, FilterChain)", "Error in doFilter.", var8);
            throw new ServletException("Error in doFilter.", var8);
        }

        if (!httpResponse.isCommitted()) {
            chain.doFilter(request, response);
        }
    }

    public void init(FilterConfig filterConfig) {
        String var2 = "init(FilterConfig)";

        try {
            this.serviceAccountName = filterConfig.getInitParameter("service_account_name");
            if (filterConfig.getInitParameter("except_list") != null) {
                this.exceptList = filterConfig.getInitParameter("except_list").split(",");
            }
        } catch (Exception var4) {
            SSOHelperUtils.getLogger().logError(CLASS_NAME + "." + "init(FilterConfig)", "Error in init.", var4);
        }

    }

    public void destroy() {
    }

    public boolean doCheckOpenToken(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws SSOException {
        return SSOHelperUtils.checkOpenToken(httpRequest, httpResponse, this.serviceAccountName);
    }

    public boolean doCheckTokenFromCookie(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws SSOException {
        return SSOHelperUtils.doCheckTokenFromCookie(httpRequest, httpResponse, this.serviceAccountName);
    }

}
